package com.douqu.game.auth.service;

import com.alibaba.fastjson.JSONObject;
import com.bean.core.util.HttpJsonUtils;
import com.douqu.game.auth.database.mapper.ClickActivationMapper;
import com.douqu.game.auth.database.model.ClickActivationModel;
import com.bean.core.util.MD5Utils;
import com.douqu.game.core.factory.PlatformFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.core.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;

/**
 * @author: miracle.
 * Description:
 * Date: 2018/5/16 0016 下午 4:47
 * Huan Yu Copyright (c) 2017 All Rights Reserved.
 */
@Service
public class ClickActivationServiceImpl implements ClickActivationService {

    @Autowired
    private ClickActivationMapper clickActivationMapper;

    private static String ext_info = "";

    @Override
    public int baiduClick(ClickActivationModel clickActivationModel) {
        //数据验证
        if (StringUtils.isNullOrEmpty(clickActivationModel.getImei())){
            return HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
        }

        //System.out.println("imei :" + clickActivationModel.getImei());

        if (StringUtils.isNullOrEmpty(clickActivationModel.getExtInfo())){
            return HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
        }
        ClickActivationModel model = clickActivationMapper.findClickActivationModel(clickActivationModel);

        ext_info = clickActivationModel.getExtInfo();
        if (model == null){
            model = new ClickActivationModel();
            model.setImei(clickActivationModel.getImei());
            model.setExtInfo(clickActivationModel.getExtInfo());
            model.setPlatform(SGCommonProto.E_PLATFORM_TYPE.PLATFORM_TYPE_BAI_DU_VALUE);
            model.setActivation(0);
            clickActivationMapper.insert(clickActivationModel);
        }
        return HttpServletResponse.SC_OK;
    }

    @Override
    public void testActivation(int platform, String oldImei) {

        if (StringUtils.isNullOrEmpty(oldImei))
            return;
        //数据库存的是 百度发来的 经过md5加密的
        //这里我们要先加密
        String imei = "";
        try {
            imei =  MD5Utils.encode(oldImei);
        }catch (Exception e){
            e.printStackTrace();
        }
        ClickActivationModel activationModel = new ClickActivationModel();
        activationModel.setImei(imei);

//        System.out.println("before:" + deviceguid);
//        System.out.println("after:" + imei);

        ClickActivationModel model = clickActivationMapper.findClickActivationModel(activationModel);
        if (model != null && !StringUtils.isNullOrEmpty(model.getExtInfo())){
            String callBackUrl = getUrl(model);
            JSONObject jsonObject = HttpJsonUtils.httpGet(callBackUrl);
            int code = jsonObject.getInteger("error_code");
            if(code != 0){
                LogUtils.error("百度激活回调出错 错误码:" + jsonObject.get("error_code") + " 错误信息:" + jsonObject.get("error_msg"));
            } else {
                LogUtils.debug("官方百度渠道 激活次数加1 设备deviceguid:" + oldImei);
                //成功够 要update
                model.setPlatform(platform);
                clickActivationMapper.update(model);
            }
        }else{
            LogUtils.error("find ClickActivationModel form sql null ");
        }
    }

    @Override
    public void testSuccess() {
        ClickActivationModel model = new ClickActivationModel();
        model.setExtInfo(ext_info);

        System.out.println(model.getExtInfo());

        if (model != null && !StringUtils.isNullOrEmpty(model.getExtInfo())){
            String callBackUrl = getUrl(model);
            JSONObject jsonObject = HttpJsonUtils.httpGet(callBackUrl);
            if(jsonObject.get("error_code").equals("0")){
                LogUtils.error("百度激活回调出错 错误码:" + jsonObject.get("error_code") + " 错误信息:" + jsonObject.get("error_msg"));
            }else {
                LogUtils.debug("官方百度渠道 激活次数加1 code:" + jsonObject.get("error_code"));
            }
        }else{
            System.out.println(123);
        }
    }

    public String getUrl(ClickActivationModel model){
        StringBuilder sb = new StringBuilder();
        sb.append(PlatformFactory.BAIDU_CALLBACK_URL);
        sb.append("&ext_info=");
        sb.append(model.getExtInfo());

        String sign = "";
        try {
            sign = MD5Utils.encode(sb.toString() + PlatformFactory.BAIDU_AKEY);
        }catch (Exception e){
            e.printStackTrace();
        }
        sb.append("&sign=");
        sb.append(sign);
        System.out.println("回调 url：" +  sb.toString());
        return sb.toString();
    }

    public static void main(String[] args) {
        try {
            String s = MD5Utils.encode("111111");
            System.out.print(s);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
